# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import xlwt
import xlrd
from xlutils.copy import copy


class ShixizengPipeline:
    row = 0

    def process_item(self, item, spider):
        list_item = list(item.values())
        if self.row == 0:
            #  创建工作蒲
            workbook = xlwt.Workbook(encoding='utf-8')
            worksheet = workbook.add_sheet('实习僧应聘信息')
            for v in list_item:
                if list_item.index(v) == len(list_item) - 1:
                    worksheet.write(self.row, list_item.index(v), ','.join(v))
                else:
                    worksheet.write(self.row, list_item.index(v), v)
            workbook.save('./shixiseng.xls')
        else:
            #  打开工作薄
            workbook = xlrd.open_workbook('./shixiseng.xls')
            new_workbook = copy(workbook)  # 将xlrd对象拷贝转化为xlwt对象
            new_worksheet = new_workbook.get_sheet(0)  # 获取转化后工作簿中的第一个表格
            for v in list_item:
                if list_item.index(v) == len(list_item) - 1:
                    new_worksheet.write(self.row, list_item.index(v), ','.join(v))
                else:
                    new_worksheet.write(self.row, list_item.index(v), v)
            new_workbook.save('./shixiseng.xls')
        self.row += 1
        return item
